package com.example.team.dao.Imp;

import com.example.team.dao.IItemDistributionDao;
import com.example.team.entity.ItemDistribution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：蓝江富
 * 时间：2023年10月31日18:53:32
 */
@Repository
public class IItemDistributionDaoImpl implements IItemDistributionDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertItemDistribution(ItemDistribution itemDistribution) {
        return jdbcTemplate.update(
                "insert into item_distribution(" +
                        "distributionId," +
                        "employeeId," +
                        "distributionDate," +
                        "itemName," +
                        "quantity," +
                        "issuer " +
                        ") " +
                        "values(?,?,?,?,?,?)"
                ,itemDistribution.getDistributionId()
                ,itemDistribution.getEmployeeId()
                ,itemDistribution.getDistributionDate()
                ,itemDistribution.getItemName()
                ,itemDistribution.getQuantity()
                ,itemDistribution.getIssuer()
        );
    }

    @Override
    public int deleteItemDistribution(ItemDistribution itemDistribution) {
        return jdbcTemplate.update(
                "delete from item_distribution " +
                "where distributionId=?",itemDistribution.getDistributionId());
    }

    @Override
    public int updateItemDistribution(ItemDistribution itemDistribution) {
        //如果更新的字段为空，则保留旧数据
        ItemDistribution itemDistribution1=getItemDistributionById(itemDistribution);
        if(itemDistribution.getEmployeeId()==null)
            itemDistribution.setEmployeeId(itemDistribution1.getEmployeeId());
        if(itemDistribution.getDistributionDate()==null)
            itemDistribution.setDistributionDate(itemDistribution1.getDistributionDate());
        if(itemDistribution.getItemName()==null)
            itemDistribution.setItemName(itemDistribution1.getItemName());
        if(itemDistribution.getQuantity()==null)
            itemDistribution.setQuantity(itemDistribution1.getQuantity());
        if(itemDistribution.getIssuer()==null)
            itemDistribution.setIssuer(itemDistribution1.getIssuer());
        return jdbcTemplate.update("update item_distribution set " +
                        "employeeId=?," +
                        "distributionDate=?," +
                        "itemName=?," +
                        "quantity=?," +
                        "issuer=? " +
                        "where distributionId=?"
                ,itemDistribution.getEmployeeId()
                ,itemDistribution.getDistributionDate()
                ,itemDistribution.getItemName()
                ,itemDistribution.getQuantity()
                ,itemDistribution.getIssuer()
                ,itemDistribution.getDistributionId()
        );
    }

    @Override
    public List<ItemDistribution> getAllItemDistribution(ItemDistribution itemDistribution) {
        if(itemDistribution.getIssuer()!=null)
            return jdbcTemplate.query("select " +
                            "distributionId," +
                            "employeeId," +
                            "distributionDate," +
                            "itemName," +
                            "quantity," +
                            "issuer " +
                "from item_distribution where issuer='"+itemDistribution.getIssuer()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(ItemDistribution.class));
        else
            return jdbcTemplate.query("select " +
                            "distributionId," +
                            "employeeId," +
                            "distributionDate," +
                            "itemName," +
                            "quantity," +
                            "issuer " +
                "from item_distribution",new Object[]{},
                new BeanPropertyRowMapper<>(ItemDistribution.class));
    }

    @Override
    public ItemDistribution getItemDistribution(ItemDistribution itemDistribution) {
        List<ItemDistribution> result=jdbcTemplate.query("select " +
                        "distributionId," +
                        "employeeId," +
                        "distributionDate," +
                        "itemName," +
                        "quantity," +
                        "issuer " +
                        "from item_distribution where issuer='"+itemDistribution.getIssuer()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(ItemDistribution.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

    @Override
    public ItemDistribution getItemDistributionById(ItemDistribution itemDistribution) {
        List<ItemDistribution> result=jdbcTemplate.query("select " +
                        "distributionId," +
                        "employeeId," +
                        "distributionDate," +
                        "itemName," +
                        "quantity," +
                        "issuer " +
                        "from item_distribution where distributionId='"+itemDistribution.getDistributionId()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(ItemDistribution.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
